home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS09.ADF / MicroEMACS / termio.c < prev    next >
C/C++ Source or Header  |  1986-05-22  |  5KB  |  210 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include <exec/types.h>
  7. #include <exec/exec.h>
  8. #include <intuition/intuition.h>
  9. #include <devices/console.h>
  10. #include <stdio.h>
  11. #include "keymap.h"
  12.  
  13. /*** CLOSE FLAGS FOR VARIOUS THINGS ***/
  14.  
  15. long   mask = 0;
  16. #define INTUITION 0x00000001
  17. #define CONSOLE   0x00000002
  18. #define SCREEN    0x00000004
  19. #define WINDOW    0x00000008
  20. #define MENU      0x00000020
  21.  
  22. struct IntuitionBase *IntuitionBase;
  23. #define INTUITION_REV 29
  24.  
  25. static struct NewWindow NewWindow = {
  26.    0, 0,
  27.    640, 200,
  28.    -1, -1,
  29.    0,
  30.    SMART_REFRESH | ACTIVATE | WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING,
  31.    NULL,
  32.    NULL,
  33.    "MEMACS 1.1",
  34.    NULL,
  35.    NULL,
  36.    100, 35,
  37.    640, 200,
  38.    WBENCHSCREEN
  39. };
  40.  
  41. struct Window *Window;
  42. static struct IOStdReq consoleIO;
  43. static struct MsgPort consoleMsgPort;
  44.  
  45. #define NOBUF 1024
  46. static char obuf[NOBUF];
  47. static int nobuf;
  48.  
  49. /*
  50.  * This function fills in the keymap string fields
  51.  */
  52. static void FillIn(keytypes, keymap, n, strings)
  53. UBYTE *keytypes;
  54. UBYTE **keymap;
  55. int n;
  56. char **strings;
  57. {
  58.    int i;
  59.  
  60.    for (i = 0; i < n; ++i)
  61.    {
  62.       if (keytypes[i] & KCF_STRING)
  63.       {
  64.          if (*strings == NULL)
  65.      {  printf("too few KeyStrings\n"); exit(1); }
  66.      keymap[i] = *strings;
  67.      ++strings;
  68.       }
  69.    }
  70.    if (*strings != NULL)
  71.    {  printf("too many KeyStrings\n"); exit(1); }
  72. }
  73.  
  74. /*
  75.  * This function is called once to set up the terminal device streams.
  76.  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
  77.  * a channel to it and sets it raw. On CPM it is a no-op.
  78.  */
  79. ttopen()
  80. {
  81.    struct Task *task;
  82.    int i;
  83.  
  84.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",
  85.                             INTUITION_REV);
  86.    if (IntuitionBase == NULL) {
  87.       (VOID) printf("no intuition here!!\n");
  88.       close_things();
  89.       exit(2);
  90.    }
  91.     mask |= INTUITION;
  92.  
  93.    Window = (struct Window *)OpenWindow(&NewWindow);
  94.    if (Window == NULL) {
  95.       (VOID) printf("could not open the window\n");
  96.       close_things();
  97.       exit(3);
  98.    }
  99.    mask |= WINDOW;
  100.  
  101.    consoleIO.io_Data = (APTR) Window;
  102.    consoleIO.io_Length = sizeof(*Window);
  103.    if (OpenDevice("console.device", 0, &consoleIO, 0) != 0) {
  104.       (VOID) printf("could not open the console\n");
  105.       close_things();
  106.       exit(3);
  107.    }
  108.    mask |= CONSOLE;
  109.  
  110.    consoleMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  111.    consoleMsgPort.mp_Flags = 0;
  112.    if ((i =  AllocSignal(-1)) == -1) {
  113.       (VOID) printf("could not alloc sig\n");
  114.       close_things();
  115.       exit(3);
  116.    }
  117.  
  118.    task = (struct Task *)FindTask(NULL);
  119.    consoleMsgPort.mp_SigBit = i;
  120.    consoleMsgPort.mp_SigTask = task;
  121.    consoleIO.io_Message.mn_ReplyPort = &consoleMsgPort;
  122.  
  123.    FillIn(HiKeyMapTypes, HiKeyMap, 40, HiStrings);
  124.    consoleIO.io_Command = CD_SETKEYMAP;
  125.    consoleIO.io_Data = (APTR) &KeyMap;
  126.    consoleIO.io_Length = sizeof(KeyMap);
  127.    DoIO(&consoleIO);
  128.  
  129.    mouse_setup_menu();
  130.    mask |= MENU;
  131.  
  132.    SetTaskPri( task, 1); /* raise priority a little as we are interactive */
  133.    mouse_enable();
  134.    nobuf = 0;
  135. }
  136.  
  137. /*
  138.  * This function gets called just before we go back home to the command
  139.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  140.  * Another no-operation on CPM.
  141.  */
  142. ttclose()
  143. {
  144.     ttflush();
  145.     close_things();
  146. }
  147.  
  148. /*
  149.  * Write a char or string to the display. On VMS, output is buffered, and
  150.  * we just put the characters in the big array, after checking for overflow.
  151.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  152.  * MS-DOS (use the very very raw console output routine).
  153.  */
  154. ttputs( s)
  155. char *s;
  156. {
  157.     while( *s) ttputc( *s++);
  158. }
  159.  
  160. ttputc(c)
  161. {
  162.         if (nobuf >= NOBUF)
  163.                 ttflush();
  164.         obuf[nobuf++] = c;
  165. }
  166.  
  167. /*
  168.  * Flush terminal buffer. Does real work where the terminal output is buffered
  169.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  170.  */
  171. ttflush()
  172. {
  173.    if (nobuf != 0)
  174.    {
  175.       register int i = nobuf;
  176.       /* The DoIO write to the console trashes D7, so we declare
  177.          an unnecessary register variable here to for it to be saved */
  178.       
  179.       consoleIO.io_Command = CMD_WRITE;
  180.       consoleIO.io_Data = (APTR) obuf;
  181.       consoleIO.io_Length = i;
  182.       DoIO(&consoleIO);
  183.       nobuf = 0;
  184.    }
  185. }
  186.  
  187. /*
  188.  * Read a character from the terminal, performing no editing and doing no echo
  189.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  190.  * simple on CPM, because the system can do exactly what you want.
  191.  */
  192. ttgetc()
  193. {
  194.    char ch;
  195.  
  196.    consoleIO.io_Command = CMD_READ;
  197.    consoleIO.io_Data = (APTR) &ch;
  198.    consoleIO.io_Length = 1;
  199.    DoIO(&consoleIO);
  200.    return((int)ch);
  201. }
  202.  
  203. static close_things()
  204. {
  205.    if (mask & MENU)     mouse_clear_menu();
  206.    if(mask & CONSOLE)   CloseDevice(&consoleIO);
  207.    if (mask & WINDOW)    (VOID) CloseWindow(Window);
  208.    if (mask & INTUITION) (VOID) CloseLibrary((VOID *)IntuitionBase);
  209. }
  210.